$(document).ready(function () {
var keyword = UrlParm.parm("keyword");

    $.ajax({
        "url":"../goods.do",
        "type":"POST",
        "data":{"method":"searchByName","keyword":keyword},
        "dataType":"json",
        "success":function(result){
            $(".card").remove();
            var i =0;
            $.each(result,function (index,data) {
                if(i==20){
                    return;
                }
                $(".products").append(showCard(data));
                i++;
            })
        }
    })
    userChange();
})
function  showCard(product) {
    return $("<div class='card'>" +
        "<a href='../xiaomi/details.html?id="+product.id+"'>"+
        "<div class='card_img'>" +
        "<img src='"+product.picture+"'>" +
        "</div>" +
        "<p class='name'>"+product.name+"</p>"+
        "<p class='price'>"+product.price+"</p>"+
        "</a></div>")
}
/**
 * 未登录不展示用户中心和购物车
 */
function  userChange() {
    $.ajax({
        "url":"../userlogin.do",
        "type":"POST",
        "data":{"method":"review"},
        "dataType":"json",
        "success":function(result){
            nameChange(result);
        }
    })
}
function nameChange(user) {
    if(user.name=='none'){
        $(".login").text("登录");
        $(".login").attr("href","../loginuser.html");
        $(".shopcar").attr("href","javascript:gotoshopcar();");
    }else {
        $(".login").text(" "+user.name+"");
        $(".login").attr("href","user.html?id="+user.id+"");
        $(".shopcar").attr("href","/xiaomi/shopcar.html")
    }
}
function gotoshopcar() {
    alert("请先登录，然后查看购物车！");
}